const api = require("../../../utils/marketAPIUtils");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:{},
    modalName:'',
    numList:[
      {name:'选择商品'},
      {name:'支付开团'},
      {name:'分享好友'},
      {name:'成功(失败退款)'},
    ],
    aloneList:[
        {name:'选择商品'},
        {name:'支付购买'},
        {name:'等待收货'},
        {name:'分享好友'},
    ],
    payType:1,
    buyType:1,
    goodsNum:1,
    specificationId:null,
    clickLock:false,
    
    order:null,
    show:false,
    timeData: {},
    
    specificationIndex:0,
    popupHeight:65,
    footBtnState:1      //底部按钮状态，1:普通，2:已参与团拼，3:被邀请参加团拼
  },

  _loadGoodsInfo:function(goodsId){
    wx.showLoading({
        title: '加载中',
        mask:true
    })
    const _self = this;
    api.post(api.url.getGoodsInfo,{
        goodsId:goodsId
    },function(res){
        console.log(res.data);
        wx.hideLoading();
        if(res.data.code == 200){
            var specificationId = null;
            if(res.data.body.specifications && res.data.body.specifications.length > 0){
                specificationId = res.data.body.specifications[0].id;
            }
            _self.setData({
                goods:res.data.body,
                specificationId:specificationId
            })
        }else if(res.data.code == 201){
            app.showErrorTips(res.data.msg,'未能获取商品信息，请稍后再试',function(){
                _self.toIndex();
            });
        }
    });
  },

  btnShowModal:function(){
    this.setData({
        modalName:'goodsSpacePanel'
    })
  },
  
  btnCloseModal:function(){
    this.setData({
        modalName:''
    })
  },

  btnUpdateGoodsNum:function(e){
    let type = e.currentTarget.dataset.type;
    if('add' == type){
        let goodsNum = parseInt(this.data.goodsNum);
        this.setData({
            goodsNum:goodsNum + 1
        })
    }else if('move' == type){
        let goodsNum = parseInt(this.data.goodsNum);
        if(goodsNum > 1){
            this.setData({
                goodsNum:goodsNum - 1
            })
        }
    }
  },

  btnGoodsPay:function(){
    if(this.data.clickLock){
        return;
    }
    this.data.clickLock = true;
    wx.showLoading({
        title: '处理中',
        mask:true
    })

    try {
      wx.setStorageSync(app.globalData.ks_buyGoods, this.data.goods);
    } catch (e) { }

    var goodsNum = this.data.goodsNum;
    var specificationId = this.data.specificationId || "";
    this.setData({
        modalName:'',
        clickLock:false
    })
    wx.hideLoading();
    wx.navigateTo({
        url:`./confirm?goodsNum=${goodsNum}&specificationId=${specificationId}&buyType=${this.data.buyType}`
    })
  },

  toIndex(){
      wx.reLaunch({
        url: '../index/index'
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options){
        let goodsId = options.goodsId;
        if(goodsId){
            this._loadGoodsInfo(goodsId)
            return
        }
    }
    wx.showToast({
        title: '参数错误',
        icon:'error'
    })
    setTimeout(() => {
        wx.reLaunch({
            url: '../index/index',
        })
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})